package com.example.peachgrove.controller.admin.works;

import com.example.peachgrove.dao.WorksMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;


@Api(tags = "文章管理")
@RestController
@RequestMapping("/create")
@ResponseBody
public class WorksController {


    @Autowired
    WorksMapper worksMapper;

    @ApiOperation("获取所文章")
    @GetMapping("/getAllWorks")
    public HashMap getAllWorks(){
        HashMap res = new HashMap();
        res.put("data",worksMapper.getAllWorks());
        return res;
    }

    @ApiOperation("根据文章id查询文章")
    @GetMapping("/getByWorkId")
    @ApiImplicitParams({
            @ApiImplicitParam(name="workId",value="id",dataTypeClass = Integer.class),
    })
    public HashMap getByWorkId(int workId){
        HashMap res = new HashMap();
        try {
            res.put("data" , worksMapper.getByWorkId(workId));
        }catch (Error e){
            res.put("Error",e);
        }
        return res;
    }

    @ApiOperation("保存博客")
    @PostMapping("/saveWork")
    @ApiImplicitParams({
            @ApiImplicitParam(name="text",value="文本",dataTypeClass = String.class, paramType = "query"),
            @ApiImplicitParam(name="userId",value="用户id",dataTypeClass = String.class, paramType = "query"),
            @ApiImplicitParam(name="desc",value="描述",dataTypeClass = String.class, paramType = "query"),
            @ApiImplicitParam(name="title",value="标题",dataTypeClass = String.class, paramType = "query"),
            @ApiImplicitParam(name="coverUrl",value="封面图url",dataTypeClass = String.class, paramType = "query"),
            @ApiImplicitParam(name="typeId",value="类型id",dataTypeClass = String.class, paramType = "query"),

    })
    public HashMap saveWork(HttpServletRequest servletRequest){
        HashMap resMap = new HashMap();
        try {
            worksMapper.saveWork(servletRequest.getParameter("userId"),
                    servletRequest.getParameter("desc"),
                    servletRequest.getParameter("title"),
                    servletRequest.getParameter("coverUrl"),
                    servletRequest.getParameter("text"),
                    servletRequest.getParameter("typeId"));
        }catch (Error e){
            resMap.put("Error",e);
        }
        return resMap;
    }

    @ApiOperation("根据用户id查询文章列表")
    @GetMapping("/getWorksByUserId")
    @ApiImplicitParams({
            @ApiImplicitParam(name="userId",value="用户id",dataTypeClass = String.class, paramType = "query")
    })
    public HashMap getWorksByUserId(HttpServletRequest servletRequest){
        HashMap resMap = new HashMap();
        try {
            List data = worksMapper.getWorksByUserId(servletRequest.getParameter("userId"));
            resMap.put("data",data);
        }catch (Error e){
            resMap.put("Error",e);
        }
        return resMap;
    }

    @ApiOperation("根据文章id修改内容")
    @PostMapping("/updateWorkById")
    @ApiImplicitParams({
            @ApiImplicitParam(name="text",value="文本",dataTypeClass = String.class, paramType = "query"),
            @ApiImplicitParam(name="userId",value="用户id",dataTypeClass = String.class, paramType = "query"),
            @ApiImplicitParam(name="desc",value="描述",dataTypeClass = String.class, paramType = "query"),
            @ApiImplicitParam(name="title",value="标题",dataTypeClass = String.class, paramType = "query"),
            @ApiImplicitParam(name="coverUrl",value="封面图url",dataTypeClass = String.class, paramType = "query"),
            @ApiImplicitParam(name="id",value="文章id",dataTypeClass = String.class, paramType = "query"),

    })
    public HashMap updateWorkById(HttpServletRequest servletRequest){
        HashMap res = new HashMap();
        int state = worksMapper.updateWorksById(servletRequest.getParameter("title"),
                servletRequest.getParameter("desc"),
                servletRequest.getParameter("text"),
                servletRequest.getParameter("coverUrl"),
                servletRequest.getParameter("typeId"),
                servletRequest.getParameter("id"));
        res.put("state",state);
        return res;
    }

    @ApiOperation("根据文章id删除内容")
    @PostMapping("/deleteWorkById")
    @ApiImplicitParams({
            @ApiImplicitParam(name="id",value="文章id",dataTypeClass = String.class, paramType = "query")
    })
    public HashMap deleteWorkById(HttpServletRequest servletRequest){
        HashMap res = new HashMap();
        int state = worksMapper.deleteWorkById(servletRequest.getParameter("id"));
        res.put("state",state);
        return res;
    }


    @ApiOperation("根据类型id查询列表")
    @GetMapping("/getWorksByTypeId")
    @ApiImplicitParams({
            @ApiImplicitParam(name="typeId",value="类型ID",dataTypeClass = String.class, paramType = "query")
    })
    public HashMap getWorksByTypeId(HttpServletRequest servletRequest){
        HashMap res = new HashMap();
        List datas = worksMapper.getWorksByTypeId(servletRequest.getParameter("typeId"));
        res.put("datas",datas);
        return res;
    }
}